import * as echarts from 'node-echarts-canvas';
import * as path from 'path';

import {
  PERCENTAGE_LOW,
  PERCENTAGE_MIDDLE,
  PERCENTAGE_HIGH,
} from '../../constants/answer-progress-state';

export const createConclusionNameList = (conclusionsResult) => {
  const conclusionNameList = conclusionsResult.map((
    currentConclusion
  ) => currentConclusion.conclusionName);
  return conclusionNameList;
};

export const createConvertedScoreList = (conclusionsResult) => {
  const cumulativeList = conclusionsResult.map((
    currentConclusion
  ) => currentConclusion.cumulative);

  return cumulativeList;
};

export const createEcharts = (
  conclusionsResult: number[],
  mergeUuid: string,
  index: number,
) => {
  const echartsConclusionNameList = createConclusionNameList(
    conclusionsResult
  );
  const echartsConvertedScoreList = createConvertedScoreList(
    conclusionsResult
  );

  // 设定宽高
  let height = 500;
  let width = 600;
  if (index === 0 || index === 3) {
    height = 300;
    width = 600;
  }
  const option = {
    color: ['#4472c4'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {},
    grid: {
      left: '1%',
      right: '4%',
      bottom: '1%',
      containLabel: true
    },
    xAxis: {
      type: 'value',
      min: 0,
      max: 100,
      minInterval: PERCENTAGE_LOW,
      axisLabel: {
        padding: [2, 2, 2, -160],
        fontSize: 24,
        formatter: (value: number) => {
          switch (value) {
            case PERCENTAGE_LOW:
              return '无症状';
            case PERCENTAGE_LOW + PERCENTAGE_MIDDLE:
              return '有症状';
            case PERCENTAGE_LOW + PERCENTAGE_MIDDLE + PERCENTAGE_HIGH:
              return '症状明显';
            default:
              return '';
          }
        }
      }
    },
    yAxis: {
      type: 'category',
      axisLabel: {
        fontSize: 24,
      },
      data: echartsConclusionNameList
    },
    series: [
      {
        type: 'bar',
        barWidth: 40,
        data: echartsConvertedScoreList
      },
    ]
  };

  const imagePath = path.resolve(__dirname, '../public/image', `${mergeUuid}-${index + 1}.png`);
  const config = {
    width,
    height,
    option,
    path: imagePath,
    enableAutoDispose: true
  };

  echarts(config);
};

const gaussianFun = (x: number): number => 130 * Math.exp(-(
  (((x - 95.97) * (x - 95.97)) / 1200)
));

export const createSolutionLineEcharts = (sumScore: number, mergeUuid: string) => {
  const generateData = () => {
    const data: Array<[number, number]> = [];
    for (let i = -200; i <= 200; i += 0.1) {
      data.push([i, gaussianFun(i)]);
    }
    return data;
  };

  const option = {
    animation: false,
    grid: {
      top: 40,
      left: 40,
      right: 40,
      bottom: 50
    },
    xAxis: {
      min: 0,
      max: 320,
      axisTick: {
        show: false
      },
      axisLabel: {
        show: false,
        fontSize: 24,
      },
    },
    yAxis: {
      min: 0,
      max: 150,
      minorTick: {
        show: false
      },
      axisLabel: {
        show: false
      },
      axisTick: {
        show: false
      },

    },
    series: [
      {
        type: 'line',
        showSymbol: false,
        data: generateData(),
        markLine: {
          symbolSize: 0,
          label: {
            fontSize: 24
          },
          data: [
            {
              name: '无症状',
              xAxis: 95.97,
              label: {
                position: 'start',
                formatter: () => 'A'
              }
            },
            {
              name: '有症状',
              xAxis: 126.32,
              label: {
                position: 'start',
                formatter: () => 'B'
              }
            },
            {
              name: '症状明显',
              xAxis: 189.28,
              label: {
                position: 'start',
                formatter: () => 'C'
              }
            },
          ]
        },
        markPoint: {
          symbol: 'pin',
          data: [
            {
              name: 'currentScore',
              coord: [sumScore, gaussianFun(sumScore)],
              value: sumScore
            }
          ]
        },
      }
    ]
  };

  const imagePath = path.resolve(__dirname, '../public/image', `${mergeUuid}-concluded.png`);
  const config = {
    width: 400,
    height: 350,
    option,
    path: imagePath,
    enableAutoDispose: true
  };

  echarts(config);
  return imagePath;
};

export const getImageFileName = (filePath) => {
  const filenameSplitedArr = filePath.split('\\');
  const imageName = filenameSplitedArr[filenameSplitedArr.length - 1];
  return imageName;
};
